﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Zatify.Domain.Abstract;
using Zatify.WebUI.Infrastructure;
using Zatify.WebUI.Infrastructure.Filters;
using Zatify.WebUI.Infrastructure.Absrtact;
using Zatify.Domain.Data;
using Zatify.WebUI.Admin.Models;

namespace Zatify.WebUI.Areas.Admin.Controllers
{
    [ZatifyAdminAuthorizeAttribute]
    [Utils.MyRequireHttps]
    public class MessageCenterController : AdminBaseController
    {
        private IZatifyRepository repository;

        protected override AdminModules AdminModule
        {
            get
            {
                return AdminModules.MessageCenter;
            }
        }

        public MessageCenterController(IZatifyRepository repository, IZatifyAuthProvider authProvider)
            //: base(authProvider)
        {
            this.repository = repository;
        }

        public ActionResult Inbox()
        {
            return View(repository.MessagesGetInboxMessages(User.Login));
        }

        public ActionResult Outbox()
        {
            return View(repository.MessagesGetOutboxMessages(User.Login));
        }

        [HttpGet]
        public ActionResult Compose()
        {
            return View(new SendNewMessageViewModel());
        }

        [HttpPost]
        public ActionResult SendMessage(SendNewMessageViewModel model)
        {
            if (ModelState.IsValid)
            {
                //TO DO: Must be created condition to determine to which Users Type to send message
                var message = new Message()
                {
                    Subject = model.Subject,
                    Body = model.Body,
                    FromSystemLogin = User.Login
                };

                switch (model.SendingToUsersType)
                {
                    case SendingToUsersType.AllUsers:
                        repository.MessagesAddToAllUsers(message);
                        break;
                    case SendingToUsersType.BusinessUsers:
                        repository.MessagesAddToBusinessUsers(message);
                        break;
                    case SendingToUsersType.EndUsers:
                        repository.MessagesAddToEndUsers(message);
                        break;
                    case SendingToUsersType.IndividualUser:
                        message.ToUserLogin = model.IndividualUserId;
                        repository.MessagesAdd(message);
                        break;
                }

                return RedirectToAction("Outbox");
            }

            return View("Compose", new SendNewMessageViewModel());
        }

        public JsonResult IsUserIDExist(string individualUserId)
        {
            var result = false;

            if (repository.Users.Any(u => u.Login.Trim().ToLower() == individualUserId.Trim().ToLower()))
                result = true;
            else if (repository.Businesses.Any(b => b.Login.Trim().ToLower() == individualUserId.Trim().ToLower()))
                result = true;
            
                return Json(result, JsonRequestBehavior.AllowGet);
        }

        [HttpGet]
        public JsonResult GetMessage(int id)
        {
            if (id > 0)
            {
                var message = repository.MessagesGetById(id);

                if (message.Status == Message.GetMessageStatus(MessageStatus.New))
                    repository.MessagesSetStatus(id, Message.GetMessageStatus(MessageStatus.Read));

                return Json(new { message = message, hasError = false }, JsonRequestBehavior.AllowGet);
            }
            else
            {
                return Json(new { message = "Error MEssage!!!", hasError = true }, JsonRequestBehavior.AllowGet);
            }
        }

        public ActionResult SendReplyMessage(int repliedMessageId, Message message)
        {
            if (ModelState.IsValid)
            {
                repository.MessagesSetStatus(repliedMessageId, Message.GetMessageStatus(MessageStatus.Replied));

                message.FromSystemLogin = User.Login;
                message.Subject = String.Format("Reply: {0}", message.Subject);

                repository.MessagesAdd(message);

                return RedirectToAction("Outbox");
            }

            return RedirectToAction("Inbox");
        }

        public ActionResult DeleteInbox(int messageId)
        {
            repository.MessagesDeleteInboxById(messageId);

            return RedirectToAction("Inbox");
        }

        public ActionResult DeleteOutbox(int messageId)
        {
            repository.MessagesDeleteOutboxById(messageId);

            return RedirectToAction("Outbox");
        }
    }
}
